package com.ktgj.flight.service.impl;

import java.util.*;

import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.flight.domain.vo.TicketRemindVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.TicketRemindMapper;
import com.ktgj.flight.domain.TicketRemind;
import com.ktgj.flight.service.ITicketRemindService;

import javax.servlet.http.HttpServletRequest;

/**
 * 提醒信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-30
 */
@Service
public class TicketRemindServiceImpl implements ITicketRemindService 
{
    @Autowired
    private TicketRemindMapper ticketRemindMapper;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询提醒信息
     * 
     * @param id 提醒信息ID
     * @return 提醒信息
     */
    @Override
    public TicketRemind selectTicketRemindById(Long id)
    {
        return ticketRemindMapper.selectTicketRemindById(id);
    }

    /**
     * 查询提醒信息列表
     * 
     * @param ticketRemind 提醒信息
     * @return 提醒信息
     */
    @Override
    public List<TicketRemind> selectTicketRemindList(TicketRemind ticketRemind)
    {
        return ticketRemindMapper.selectTicketRemindList(ticketRemind);
    }

    //查询儿童婴儿购票说明
    @Override
    public List<Map<String, Object>> queryTicketRemind() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<TicketRemind> ticketReminds = this.ticketRemindMapper.selectTicketRemindList(null);
        ticketReminds.forEach(ticketRemind -> {
            Map<String, Object> map = new HashMap<>();
            if (ticketRemind.getRemindType().equals("7")) {
                map.put("remindName", ticketRemind.getRemindName());
                map.put("remindDesc", ticketRemind.getRemindDesc());
                mapList.add(map);
            }
        });
        return mapList;
    }

    /**
     * 新增提醒信息
     * 
     * @param ticketRemind 提醒信息
     * @return 结果
     */
    @Override
    public int insertTicketRemind(TicketRemind ticketRemind)
    {
        ticketRemind.setCreateTime(DateUtils.getNowDate());
        return ticketRemindMapper.insertTicketRemind(ticketRemind);
    }

    /**
     * 修改提醒信息
     * 
     * @param ticketRemind 提醒信息
     * @return 结果
     */
    @Override
    public int updateTicketRemind(TicketRemind ticketRemind)
    {
        ticketRemind.setUpdateTime(DateUtils.getNowDate());
        return ticketRemindMapper.updateTicketRemind(ticketRemind);
    }

    /**
     * 批量删除提醒信息
     * 
     * @param ids 需要删除的提醒信息ID
     * @return 结果
     */
    @Override
    public int deleteTicketRemindByIds(Long[] ids)
    {
        return ticketRemindMapper.deleteTicketRemindByIds(ids);
    }

    /**
     * 删除提醒信息信息
     * 
     * @param id 提醒信息ID
     * @return 结果
     */
    @Override
    public int deleteTicketRemindById(Long id)
    {
        return ticketRemindMapper.deleteTicketRemindById(id);
    }

    /**
     * 机票提醒信息列表
     * */
    @Override
    public List<TicketRemind> selectTicketRemindList(String state, String remindType, String remindName, String time, String timeBack) {
        List<TicketRemind> ticketReminds = ticketRemindMapper.ticketRemindList(state, remindType, remindName, time, timeBack);
        if (ticketReminds != null && ticketReminds.size() > 0){
            for (TicketRemind ticketRemind : ticketReminds) {
                if (ticketRemind.getRemindType().equals("1")){
                    ticketRemind.setRemindType("首页提醒");
                }
                else if (ticketRemind.getRemindType().equals("2")){
                    ticketRemind.setRemindType("航司提醒");
                }
                else if (ticketRemind.getRemindType().equals("4")){
                    ticketRemind.setRemindType("特别提醒");
                }

                if (ticketRemind.getState().equals("0")){
                    ticketRemind.setState("失效");
                }
                else if (ticketRemind.getState().equals("1")){
                    ticketRemind.setState("启用");
                }
            }
        }
        return ticketReminds;
    }

    /**
     * 创建机票提醒消息信息
     * */
    @Override
    public int addTicketRemind(HttpServletRequest request, TicketRemindVo ticketRemindVo) throws Exception {
        String token = request.getHeader("token");
        Employee employee = remoteCustomerService.getLoginEmployeeByToken(token).getData();
        if (employee != null){
            ticketRemindVo.setCreateEmployee(employee.getEmployeeName());
        }
        else {
            throw new Exception("未查到员工信息");
        }

        if (ticketRemindVo.getAirlineList() != null && ticketRemindVo.getAirlineList().size() > 0){
            for (int i = 0; i < ticketRemindVo.getAirlineList().size(); i++) {
                if (i == 0){
                    ticketRemindVo.setFlight(ticketRemindVo.getAirlineList().get(i));
                }
                else {
                    ticketRemindVo.setFlight(ticketRemindVo.getFlight() + "|" + ticketRemindVo.getAirlineList().get(i));
                }
            }
        }

        ticketRemindVo.setCreateTime(new Date());
        int i = ticketRemindMapper.addTicketRemind(ticketRemindVo);
        return i;
    }
}
